package com.qiyue.admin.biz.base;

import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.qiyue.admin.entity.base.User;
import com.qiyue.admin.repository.base.UserRepository;

import com.qiyue.api.vo.user.UserInfo;
import com.qiyue.common.biz.BaseBiz;
import com.qiyue.common.constant.UserConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2018/1/23.
 * Description :
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class UserBiz   extends BaseBiz<UserRepository,User,Long> {





    @Override
    @CacheClear(pre="user{1.username}")
    public User save(User entity) {
//        String password = new BCryptPasswordEncoder(UserConstant.PW_ENCORDER_SALT).encode(entity.getPassword());
//        entity.setPassword(password);
        return super.save(entity);
    }

    /**
     * 根据 过滤器获取用户信息
     * @param filters
     * @return
     */
    @Cache(key="user{1}")
    public User getUserByfilters(String filters){
        return  this.findOneByFilter(filters);
    }

    /**
     * 根据 过滤器获取用户信息
     * @param account
     * @return
     */
    @Cache(key="user{1}")
    public User getUserByAccout(String account){
        String filters="EQ_account="+account;
        return  this.findOneByFilter(filters);
    }

    /**
     * 根据 过滤器获取用户信息
     * @param params
     * @return
     */
    @Cache(key="user{1}")
    public User getUserByMap(Map<String,Object> params){
        return  this.findOneByFilter(params);
    }

    /**
     * 查询该群组拥有的用户
     * @param groupId
     * @return
     */
    public List<User> queryMemberByGroupId(Long groupId){
        String filters="EQ_groupMember.groupId="+groupId;
       return  this.search(filters);

    }

    /**
     * 查询群组的领导用户
     * @return
     */
    public List<User> queryLeaderByGroupId(Long groupId){
        String filters="EQ_groupLeader.groupId="+groupId;
        return  this.search(filters);
    }
}
